﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo.Framework.Strings.DuplicateChar
{
    class StreamManager
    {
        private Stream _stream;

        public StreamManager(List<char> charSet)
        {
            try
            {
                _stream = new Stream(charSet);
            }
            catch (Exception ex)
            {                
                throw;
            }            
        }

        public char GetFirstUnique()
        {
            try
            {
                Dictionary<char, int> charStats = new Dictionary<char,int>();

                while (_stream.HasNext())
                {
                    char c = _stream.GetNext();
                    if (charStats.ContainsKey(c))
                    {
                        charStats[c] = charStats[c] + 1;
                    }
                    else
                    {
                        charStats[c] = 1;
                    }
                }
                                
                if (charStats.Count > 0)
                {                                       
                    char returnValue = charStats.Where(c => c.Value == 1).FirstOrDefault().Key;
                    if (returnValue == Char.MinValue)
                    {
                        throw new Exception("No Unique elements found");
                    }                    
                    return returnValue;
                }
                else
                {
                    throw new Exception("No elements found were");
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    
   
    }
}
